package com.example.myapplication.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.myapplication.Person;
import java.util.ArrayList;

public class PersonDataBase {
  private SQLiteDatabase database;
  private SqlLiteHelper dataBaseHelper;

  public PersonDataBase(Context context) {
    dataBaseHelper = new SqlLiteHelper(context);
  }
  public void open() {
    database = dataBaseHelper.getWritableDatabase();
  }

  public void close() {
    dataBaseHelper.close();
  }

  public int InsertSingleData(String name, String job) {
    if (!isRecordExits(name)) {
      ContentValues values = new ContentValues();
      values.put("name", name);
      values.put("job", job);
      int number = (int) database.insert("user", null, values);
      if (number > 0) {
        System.out.println("输入成功");
        return 1;
      } else {
        System.out.println("输入失败");
        return -1;
      }
    }
    return 0;
  }
  public void InsertMultiData(ArrayList<Person> personArrayList) {

    //使用事务，将所有数据一次性插入到数据库中

    try {

      database.beginTransaction();
      ContentValues values = new ContentValues();
      for (Person person : personArrayList) {
        values.put("name", person.getName());
        values.put("job", person.getJob());
        database.insert("user", null, values);
        values.clear();

      }
      database.setTransactionSuccessful();
      System.out.println("插入成功了");
    } catch (Exception e) {
      throw new RuntimeException(e);

    }finally {

      database.endTransaction();//最后将数据一次性插入表中

    }


  }

  public ArrayList<Person> ShowAllData() {
    Cursor cursor = database.query("user", null, null, null, null, null, null);
    ArrayList<Person> personArrayList = new ArrayList<>();

    if (cursor != null && cursor.moveToFirst()) {
      do {
        int nameIndex=cursor.getColumnIndex("name");
        int jobIndex=cursor.getColumnIndex("job");
        if(nameIndex==-1||jobIndex==-1)
          return personArrayList;
        String name = cursor.getString(nameIndex);
        String job = cursor.getString(jobIndex);
        Person person = new Person(name, job);
        personArrayList.add(person);

      }while (cursor.moveToNext());
    }

    return personArrayList;
  }

  public int UpdateSinglePerson(String name,String newJob) {

    if (isRecordExits(name)) {
      ContentValues values = new ContentValues();
      values.put("job", newJob);

      //更新满足指定条件的内容
      int nums = database.update("user", values, "name=?", new String[] { name });
      if (nums > 0) {
        System.out.println("更新成功");
        return 1;
      } else {
        System.out.println("更新失败");
        return -1;
      }
    } else {
      System.out.println("数据不存在");
      return 0;
    }


  }

  public boolean isRecordExits(String name) {


    String[] projection = { "name" };//列名
    String selection = "name=?";//whereClause参数，?号为占位符
    String[] selectionArgs = { name };//实际的参数

    Cursor cursor = database.query("user", projection, selection, selectionArgs, null, null, null);

    boolean recordExits=cursor.moveToFirst();
    cursor.close();

    return recordExits;
  }
  public int DeleteSinglePerson(String name) {
    if (isRecordExits(name)) {

      int nums = database.delete("user", "name=?", new String[] { name });
      if (nums > 0) {
        System.out.println("删除数据成功");
        return 1;
      } else {
        System.out.println("删除数据失败");
        return -1;
      }
    } else {
      System.out.println("数据不存在");
      return 0;
    }
  }
}
